package com.PVZ.utils;

import com.PVZ.engine.map.GameMap;
import com.PVZ.engine.map.MapBlock;
import com.PVZ.model.Anime;
import com.PVZ.model.Entity;
import com.PVZ.model.emuns.AnimeType;
import com.PVZ.model.zombie.Zombie;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by @Author: kabuda on @Date: 2021/5/18 19:17
 */
public class Util {

    //常量部分

    //向日葵选项卡的位置矩形
    public static final Rectangle SUNREC = new Rectangle(0, 80, 80, 80);

    //豌豆射手选项卡
    public static final Rectangle PEAREC = new Rectangle(0, 80, 80, 80);

    /**
     * 将行列解析成坐标
     *
     * @param row 行
     * @param col 列
     * @return 坐标
     */
    public static Point parseRanksToPoint(int row, int col) {
        return GameMap.blocks[row][col].getPoint();
    }

    public static int parseXtoRow(int x){
        return x-GameMap.startX/MapBlock.width;
    }

    public static int parseYtoCol(int y){
        return y-GameMap.startY/MapBlock.height;
    }


    /**
     * 通过行和列找到单元格
     * @param row 行
     * @param col 列
     * @return 单元格
     */
    public static MapBlock getBlockByCoor(int row, int col) {
        if(row<0||col<0||row>=GameMap.MAX_ROW||col>=GameMap.TOTAL_COLUMN){
            return null;
        }

        return GameMap.blocks[row][col];
    }

    /**
     * 通过坐标找到单元格
     * ----可提高速度--待修改
     * @param point 坐标
     * @return 单元格
     */
    public static MapBlock getBlockByPoint(Point point){
        for (MapBlock[] blocks : GameMap.blocks) {
            for (MapBlock block : blocks) {
                if(isPointInBlock(point,block)){
                    return block;
                }
            }
        }
        return null;
    }

    /**
     *  判断该坐标在不在该单元格里
     * @param point 传入坐标
     * @param block 传入单元格
     * @return 是否
     */
    public static boolean isPointInBlock(Point point, MapBlock block) {
        if (point.getX() >= block.getPoint().x && point.getX() < block.getPoint().x+ MapBlock.width) {
            return point.getY() >= block.getPoint().y && point.getY() < block.getPoint().y + MapBlock.height;
        }

        return false;
    }



    // /**
    //  * 将image对象 转成 BufferedImage
    //  *
    //  * @param image Image对象
    //  * @return BufferedImage
    //  */
    // public static BufferedImage toBufferedImage(Image image) {
    //     if (image instanceof BufferedImage) {
    //         return (BufferedImage) image;
    //     }
    //     image = new ImageIcon(image).getImage();
    //     BufferedImage bimage = null;
    //     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    //     try {
    //         int transparency = Transparency.OPAQUE;
    //         GraphicsDevice gs = ge.getDefaultScreenDevice();
    //         GraphicsConfiguration gc = gs.getDefaultConfiguration();
    //         bimage = gc.createCompatibleImage(
    //                 image.getWidth(null), image.getHeight(null), transparency);
    //     } catch (HeadlessException e) {
    //         //........
    //     }
    //
    //     if (bimage == null) {
    //         int type = BufferedImage.TYPE_INT_RGB;
    //         bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    //     }
    //     Graphics g = bimage.createGraphics();
    //
    //     g.drawImage(image, 0, 0, null);
    //     g.dispose();
    //
    //     return bimage;
    // }

    /**
     * 从文件夹中读取图片到list中
     * @param startIndex 起始下标
     * @param endIndex 结束下标
     * @param src 文件夹路径
     * @return list
     */
    public static List<String> addImageToList(int startIndex,int endIndex,String src,String name){
        List<String> list = new ArrayList<>();
        for (int i = startIndex; i <= endIndex; i++) {
            //每帧图片的地址
            String picPath = src + "/"+name+"-" + i + ".png";
            list.add(picPath);
        }

        return list;
    }

    public static Anime createAnime(String src, String name, int starIndex, int endIndex, int playMode) {
        Anime anime = new Anime();

        List<String> animeList = addImageToList(starIndex, endIndex, src, name);


        anime.setAnimeList(animeList);
        anime.setCurPic(animeList.get(0));
        anime.setAnimePlayMode(playMode);

        return anime;

    }


    public static void changeAnime(Entity entity, AnimeType animeType){

        Anime anime = entity.getAnimeMap().get(animeType);
        Map<AnimeType, Anime> animeMap=new HashMap<>();
        Anime changeAnime = null;



        if(entity instanceof Zombie){

            switch (((Zombie) entity).getZombieType()){
                case ConeheadZombie:
                    if(animeType==AnimeType.ATTACK){
                        String src = "res/img/zombiePng/NormalZombie/attack";

                        int starIndex = 0;
                        int endIndex = 20;

                        changeAnime = Util.createAnime(src, "Attack", starIndex, endIndex, -1);

                    }else if(animeType ==AnimeType.MOVE){
                        String src="res/img/zombiePng/NormalZombie/move";

                        int starIndex=0;
                        int endIndex=30;

                        changeAnime= Util.createAnime(src, "Move", starIndex, endIndex, -1);

                    }break;
                case PaperZombie:
                    if(animeType==AnimeType.ATTACK){
                        String src="res/img/zombiePng/PaperZombie/eat_nopaper";

                        int starIndex=1;
                        int endIndex=29;

                        changeAnime = Util.createAnime(src, "PaperZombie_eat_nopaper", starIndex, endIndex, -1);
                    }else if(animeType ==AnimeType.MOVE){
                        String src="res/img/zombiePng/PaperZombie/move_nopaper";

                        int starIndex=1;
                        int endIndex=41;

                        changeAnime = Util.createAnime(src, "PaperZombie_move_nopaper", starIndex, endIndex, -1);

                    }break;

                case BucketZombie:
                    if(animeType==AnimeType.ATTACK){
                        String src = "res/img/zombiePng/NormalZombie/attack";

                        int starIndex = 0;
                        int endIndex = 20;

                        changeAnime= Util.createAnime(src, "Attack", starIndex, endIndex, -1);
                    }else if(animeType ==AnimeType.MOVE){
                        String src="res/img/zombiePng/NormalZombie/move";

                        int starIndex=0;
                        int endIndex=30;

                        changeAnime= Util.createAnime(src, "Move", starIndex, endIndex, -1);

                    }break;

                case FlagZombie:
                    if(animeType==AnimeType.ATTACK){
                        String src="res/img/zombiePng/FlagZombie/attack";

                        int starIndex=0;
                        int endIndex=20;

                        changeAnime = Util.createAnime(src, "Attack", starIndex, endIndex, -1);
                    }else if(animeType ==AnimeType.MOVE){
                        String src="res/img/zombiePng/FlagZombie/move";

                        int starIndex=0;
                        int endIndex=11;

                        changeAnime= Util.createAnime(src, "Move", starIndex, endIndex, -1);

                    }break;

                case FootballZombie:
                    if(animeType==AnimeType.ATTACK){
                        String src="res/img/zombiePng/FootballZombie/eat";

                        int starIndex=1;
                        int endIndex=36;

                        changeAnime = Util.createAnime(src, "Zombie_football", starIndex, endIndex, -1);
                    }else if(animeType ==AnimeType.MOVE){
                        String src="res/img/zombiePng/FootballZombie/move";

                        int starIndex=1;
                        int endIndex=30;

                        changeAnime= Util.createAnime(src, "Zombie_football", starIndex, endIndex, -1);

                    }break;
                case NormalZombie:
                    if(animeType==AnimeType.ATTACK){
                        String src = "res/img/zombiePng/NormalZombie/attack";

                        int starIndex = 0;
                        int endIndex = 20;

                        changeAnime = Util.createAnime(src, "Attack", starIndex, endIndex, -1);
                    }else if(animeType ==AnimeType.MOVE){
                        String src="res/img/zombiePng/NormalZombie/move";

                        int starIndex=0;
                        int endIndex=30;

                        changeAnime =Util.createAnime(src, "Move", starIndex, endIndex, -1);
                    }break;

                default:break;
            }

        }

        entity.getAnimeMap().put(animeType,changeAnime);


//        animeMap.put(animeType,changeAnime);
//        entity.setAnimeMap(animeMap);


//       entity.getAnimeMap().put(animeType,changeAnime);
//        entity.setAnimeMap(entity.getAnimeMap());

        System.out.println();

    }
}
